﻿using System;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;
using System.Threading;

namespace pping
{
    internal class Program
    {
        private static string _priceStr = "";
        private static string _netway = "";
        private static string _openValue = "";
        private static string _highValue = "";
        private static string _lowValue = "";
        private static string _inputstr = "";
        private static bool _tModle = false;
        private static int _stockCode;
        static void Main(string[] args)
        {
            Console.Clear();
            if (args.Length != 0)
            {
                _inputstr = args[0].ToString();
                foreach (string text in args)
                {
                    if (text == "-t")
                    {
                        _tModle = true;
                    }
                }
            }
            if (_tModle)
            {
                if (_inputstr != "")
                {
                    if (_inputstr.Length < 7)
                    {
                        if (int.TryParse(_inputstr, out _stockCode))
                        {
                            getnetipT();
                        }
                        else
                        {
                            Console.WriteLine("输入无效。");
                        }
                    }
                    else
                    {
                        Console.WriteLine("输入无效。");
                    }
                }
                else
                {
                    Console.WriteLine("输入无效。");
                }
            }
            else if (_inputstr != "")
            {
                if (_inputstr.Length < 7)
                {
                    if (int.TryParse(_inputstr, out _stockCode))
                    {
                        getnetip();
                    }
                    else
                    {
                        Console.WriteLine("输入无效。");
                    }
                }
                else
                {
                    Console.WriteLine("输入无效。");
                }
            }
            else
            {
                Console.WriteLine("输入无效。");
            }
        }

        private static void getnetipT()
        {
            try
            {
                NetworkInterface[] allNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
                NetworkInterface[] array = allNetworkInterfaces;
                foreach (NetworkInterface networkInterface in array)
                {
                    if (networkInterface.OperationalStatus != OperationalStatus.Up || networkInterface.NetworkInterfaceType == NetworkInterfaceType.Loopback || networkInterface.GetIPProperties() == null || networkInterface.GetIPProperties().GatewayAddresses.Count <= 0)
                    {
                        continue;
                    }
                    foreach (GatewayIPAddressInformation gatewayAddress in networkInterface.GetIPProperties().GatewayAddresses)
                    {
                        if (gatewayAddress != null && gatewayAddress.Address != null)
                        {
                            Console.WriteLine("正在 Ping " + gatewayAddress.Address.ToString() + " 具有 32 字节的数据:");
                            _netway = gatewayAddress.Address.ToString();
                        }
                    }
                }
                GetPriceT();
            }
            catch (Exception ex)
            {
                Console.WriteLine("发生错误：" + ex.Message);
            }
        }


        private static void getnetip()
        {
            try
            {
                NetworkInterface[] allNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
                NetworkInterface[] array = allNetworkInterfaces;
                foreach (NetworkInterface networkInterface in array)
                {
                    if (networkInterface.OperationalStatus != OperationalStatus.Up || networkInterface.NetworkInterfaceType == NetworkInterfaceType.Loopback || networkInterface.GetIPProperties() == null || networkInterface.GetIPProperties().GatewayAddresses.Count <= 0)
                    {
                        continue;
                    }
                    foreach (GatewayIPAddressInformation gatewayAddress in networkInterface.GetIPProperties().GatewayAddresses)
                    {
                        if (gatewayAddress != null && gatewayAddress.Address != null)
                        {
                            Console.WriteLine("正在 Ping " + gatewayAddress.Address.ToString() + " 具有 32 字节的数据:");
                            _netway = gatewayAddress.Address.ToString();
                        }
                    }
                }
                GetPrice();
            }
            catch (Exception ex)
            {
                Console.WriteLine("发生错误：" + ex.Message);
            }
        }
        private static void GetPrice()
        {
            //IL_0058: Unknown result type (might be due to invalid IL or missing references)
            //IL_005f: Expected O, but got Unknown
            string text = ((_stockCode > 600000) ? "sh" : "sz");
            string text2 = ((_stockCode >= 600000) ? _stockCode.ToString() : _stockCode.ToString("D6"));
            string text3 = "http://qt.gtimg.cn/q=" + text + text2;
            HttpClient val = new HttpClient();
            try
            {
                string response = (_priceStr = val.GetStringAsync(text3).Result);
                string text4 = ParseStockPrice(response);//现价
                string text5 = ParseStockOpen(response);//开盘价
                string text6 = ParseStockHigh(response);//最高价
                string text7 = ParseStockLow(response);//最低价
                string text8 = ParseStockPercent(response);//涨跌幅
                Console.WriteLine("来自 " + _netway + " 的回复: 字节=32 时间=" + text4 + "ms TTL=254");
                Console.WriteLine("来自 " + _netway + " 的回复: 字节=32 时间=" + text5 + "ms TTL=254");
                Console.WriteLine("来自 " + _netway + " 的回复: 字节=32 时间=" + text6 + "ms TTL=254");
                Console.WriteLine("来自 " + _netway + " 的回复: 字节=32 时间=" + text7 + "ms TTL=254");
                Console.WriteLine("请求超时。");
                Console.WriteLine(_netway + " 的 Ping 统计信息:\r\n    数据包: 已发送 = 5，已接收 = 4，丢失 = 1 (" + text8 + "% 丢失)，\r\n往返行程的估计时间(以毫秒为单位):");
                Console.WriteLine("    最短 = " + text7 + "ms，最长 = " + text6 + "ms，平均 = " + text4 + "ms");
            }
            catch (Exception)
            {
                Console.WriteLine("Error: 请检查输入的代码");
            }
            finally
            {
                ((IDisposable)val)?.Dispose();
            }
        }
        private static void GetPriceT()
        {
            //IL_005e: Unknown result type (might be due to invalid IL or missing references)
            //IL_0065: Expected O, but got Unknown
            string text = ((_stockCode > 600000) ? "sh" : "sz");
            string text2 = ((_stockCode >= 600000) ? _stockCode.ToString() : _stockCode.ToString("D6"));
            string text3 = "http://qt.gtimg.cn/q=" + text + text2;
            while (true)
            {
                HttpClient val = new HttpClient();
                try
                {
                    string text4 = ParseStockPrice(_priceStr = val.GetStringAsync(text3).Result);
                    Console.WriteLine("来自 " + _netway + " 的回复: 字节=32 时间=" + text4 + "ms TTL=254");
                    Thread.Sleep(1000);
                }
                catch (Exception)
                {
                    Console.WriteLine("Error: 请检查输入的代码");
                }
                finally
                {
                    ((IDisposable)val)?.Dispose();
                }
            }
        }
        private static string ParseStockHigh(string response)
        {
            int num = response.IndexOf('"') + 1;
            int num2 = response.LastIndexOf('"');
            string text = response.Substring(num, num2 - num);
            string[] array = text.Split('~');
            return array[33];
        }
        private static string ParseStockLow(string response)
        {
            int num = response.IndexOf('"') + 1;
            int num2 = response.LastIndexOf('"');
            string text = response.Substring(num, num2 - num);
            string[] array = text.Split('~');
            return array[34];
        }
        private static string ParseStockOpen(string response)
        {
            int num = response.IndexOf('"') + 1;
            int num2 = response.LastIndexOf('"');
            string text = response.Substring(num, num2 - num);
            string[] array = text.Split('~');
            return array[5];
        }
        private static string ParseStockPercent(string response)
        {
            int num = response.IndexOf('"') + 1;
            int num2 = response.LastIndexOf('"');
            string text = response.Substring(num, num2 - num);
            string[] array = text.Split('~');
            return array[32];
        }
        private static string ParseStockPrice(string response)
        {
            Match match = Regex.Match(response, "~(\\d+\\.\\d+)~");
            return match.Success ? match.Groups[1].Value : "请求超时。";
        }
        private static string ParseStockUp(string response)
        {
            int num = response.IndexOf('"') + 1;
            int num2 = response.LastIndexOf('"');
            string text = response.Substring(num, num2 - num);
            string[] array = text.Split('~');
            return array[31];
        }

    }
}
